<?php
/*
 * СПВ
 **/
class SP_Form_CommonField
{
	protected $name;
	protected $value;
	protected $default;
	protected $label;
	protected $disabled=false;
	protected $validator;
	protected $attr=array();
	protected $errors=array();
	protected $form_name;
	protected $input=1;
	protected $return_type;
	protected $input_filters=array();
	protected $output_filters=array();
	protected $js;
	const INPUT_POST=1;
	const INPUT_GET=2;
	const INPUT_REQUEST=3;
	
	public function __construct($attr=null)
	{
		if(is_array($attr))
		{
			foreach($attr as $key => $value)
			{
				$this->$key=$value;
			}
		}
	}

	public function getName()
	{
		return $this->name;
	}
	
	public function setName($value)
	{
		$this->name=$value;
		return $this;
	}
	
	public function getFullName()
	{
		$form_name=$this->getFormName();
		$name = empty($form_name)? $this->getName() : $form_name.'['.$this->getName().']';
		return $name;
	}
	
	public function setValue($value)
	{
		$this->value=$value;
		return $this;
	}
	
	public function getValue()
	{
		$value=$this->applyType($this->value);
		return $value;
	}
	
	protected function applyType($value)
	{
		if( $this->return_type ) settype($value, $this->return_type);
		return $value;
	}

	public function filtrateInput($value)
	{
		foreach( $this->input_filters as $filter )
		{
			$value=$filter->process($value);
		}
		return $value;
	}
	
	public function setDefault($value)
	{
		$this->default=$value;
		return $this;
	}
	
	public function getDefault()
	{
		return $this->default;
	}
	
	public function setLabel($value)
	{
		$this->label=$value;
		return $this;
	}
	
	public function getLabel()
	{
		if( !empty($this->label) )
		{
			if( $this->getFormName() ) $prefix=$this->getFormName().'_';
			$out='<label for="'.$this->generateId().'">'.$this->label.'</label>';
		}
		return $out;
	}
	
	public function getForHtml()
	{
		$value=$this->getValue();
		$default=$this->getDefault();
		$value = ($value===null)? $default : $value;
		if($this->disabled) $disabled=' disabled="disabled"';
		$out=array(
			'id'=>$this->generateId(),
			'name'=>$this->getFullName(),
			'attr'=>$this->compileAttr(),
			'value'=>$value,
			'disabled'=>$disabled,
		);
		return $out;
	}
	
	public function setValidator($value)
	{
		$this->validator=$value;
		return $this;
	}
	
	public function getValidator()
	{
		return $this->validator;
	}
	
	public function setAttr($value)
	{
		$this->attr=$value;
		return $this;
	}
	
	public function getAttr()
	{
		return $this->attr;
	}
	
	public function addAttr($name, $value)
	{
		$this->attr[$name]=$value;
		return $this;
	}
	
	public function getErrors()
	{
		return $this->errors;
	}
	
	public function setErrors($value)
	{
		$this->errors=(array)$value;
		return $this;
	}
	
	public function getError($delimiter='<br />')
	{
		$errors = $this->getErrors();
		if(count($errors)==0) return '';
		if(count($errors)==1) return $this->errors[0];
		return SP_Common_Functions::arrayToString($this->errors, $delimiter);
	}
	
	public function setFormName($value)
	{
		$this->form_name=$value;
		return $this;
	}
	
	public function getFormName()
	{
		return $this->form_name;
	}
	
	public function setInput($value)
	{
		$this->input=$value;
		return $this;
	}
	
	public function setJs($value)
	{
		$this->js=$value;
	}
	
	public function getJs()
	{
		return $this->js;
	}
	
	public function addInputFilter($name, $value)
	{
		$this->input_filters[$name]=$value;
		return $this;
	}
	
	public function addOutputFilter($value, $name)
	{
		$this->output_filters[$name]=$value;
		return $this;
	}
	
	public function __toString()
	{
		return $this->getHtml();
	}
	
	protected function &getInputArray()
	{
		if( $this->input==self::INPUT_POST )
		{
			$input = new SP_Helper_Post;
			//return $_POST;
		}
		elseif( $this->input==self::INPUT_GET )
		{
			$input = new SP_Helper_Get;
			//return $_GET;
		}
		elseif( $this->input==self::INPUT_REQUEST )
		{
			$input = new SP_Helper_Request;
			//return $_REQUEST;
		}
		return $input;
	}
	
	public function validate()
	{
		if( $this->validator )
		{
			$this->validator->setInput($this->value);
			if ( !$this->validator->validate() )
			{
				$this->errors=$this->validator->getResultErrors();
				return false;
			}
		}
		return true;
	}
	
	public function getRequestValue()
	{
		$form_name=$this->getFormName();
		$array=$this->getInputArray();
		if( empty($form_name) )
		{
			//return $this->value=$array[$this->getName()];
			return $array->get($this->getName());
		}
		else
		{
			//return $this->value=$array[$form_name][$this->getName()];
			$array=$array->get($form_name);
			return $array[$this->getName()];
		}
	}
	
	public function setFromRequest()
	{
		$this->setValue( $this->getRequestValue() );
	}
	
	public function generateId()
	{
		if( $this->getFormName() ) $prefix=$this->getFormName().'_';
		$out=$prefix.$this->getName().'_id';
		return $out;
	}
	
	protected function compileAttr()
	{
		return SP_Helper_Html::makeAttributes($this->attr);
	}
	
}